import { createStore } from 'redux'
import { increment, decrement } from './actions/counter'
import counterReducer from './reducers/counter'

// !#1 接收 reducer，返回 store
const store = createStore(counterReducer)

// !#2 获取状态
console.log(store.getState()) // 10

// !先订阅
const unSubscribe = store.subscribe(() => {
  // 只要有人 dispatch 了 action，这儿就会触发
  console.log(store.getState())
})
// unSubscribe()

// !#3 如何更新状态？store.dispatch(action)
// 一旦进行了 dispatch(action)，reducer 就会自动执行并更新状态，更新完毕后的状态又交给了 store
// store.dispatch({ type: 'INCREMENT', payload: 3 })
// 优化上一行代码，increment 是一个 actionCreator，它返回的结果就是 action => { type: 'INCREMENT', payload: 3 }
/* store.dispatch(increment(3))
console.log(store.getState()) // 13

store.dispatch(decrement(4))
console.log(store.getState()) // 9

store.dispatch(decrement(1))
console.log(store.getState()) // 8 */
store.dispatch(increment(3))
store.dispatch(decrement(4))
store.dispatch(decrement(1))
